package com.ruoyi.web.controller.sujian;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.BillInfo;
import com.ruoyi.system.service.IBillInfoService;
import com.ruoyi.system.service.IBillInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * 国贸租户管理Controller
 *
 * @author xty
 * @date 2021-12-07
 */
@Controller
@RequestMapping("/sujian/billManagement")
public class BillManController extends BaseController
{
    private String prefix = "propertys/sujian/billManagement";

    @Autowired
    private IBillInfoService billInfoService;

    /*进入首页*/
    @GetMapping()
    public String bill()
    {
        return prefix + "/main";
    }

    /**
     * 查询所有账单列表
     */
//    @RequiresPermissions("property:tenant:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BillInfo billInfo)
    {
        startPage();
        List<BillInfo> list =billInfoService.selectBillInfoList(billInfo);
        return getDataTable(list);
    }

    /**
     * 新增账单管理
     * 页面跳转
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存账单管理
     * 保存新增
     */
//    @RequiresPermissions("property:tenant:add")
    @Log(title = "账单管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BillInfo billInfo)
    {
        return toAjax(billInfoService.insertBillInfo(billInfo));
    }



    /**
     * 修改账单管理
     * 页面跳转
     */
    @GetMapping("/edit/{billId}")
    public String edit(@PathVariable("billId") String billId, ModelMap modelMap)
    {
        BillInfo billInfo = billInfoService.selectBillInfoById(billId);
        modelMap.put("billInfo", billInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存账单管理
     * 保存修改
     */
//    @RequiresPermissions("property:tenant:edit")
    @Log(title = "账单管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BillInfo billInfo)
    {
        return toAjax(billInfoService.updateBillInfo(billInfo));
    }


    /**
     * 删除账单管理
     */
    @Log(title = "账单管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(billInfoService.deleteBillInfoByIds(ids));
    }

    /**
     * 导入账单列表
     * */
    @Log(title = "账单管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importExcel")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<BillInfo> util = new ExcelUtil<>(BillInfo.class);
        List<BillInfo> lists = util.importExcel(file.getInputStream());
        SysUser sysUser = ShiroUtils.getSysUser();
        //校验数据
        String res = billInfoService.validateDatas(lists);
        if (StringUtils.isNotBlank(res)){
            return AjaxResult.error(res);
        }
        String message =billInfoService.importRelations(lists,updateSupport,sysUser);
        return AjaxResult.success(message);
    }

    /**
     * 导入中的下载模板
     * */
//    @RequiresPermissions("property:tenant:view")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<BillInfo> util = new ExcelUtil<BillInfo>(BillInfo.class);
        return util.importTemplateExcel("账单信息表");
    }



    /**
     * 导出账单管理列表
     */
//    @RequiresPermissions("property:tenant:export")
    @Log(title = "账单管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BillInfo billInfo,String ids)
    {
        List<BillInfo> list = new ArrayList<>();
        if(StringUtils.isNotEmpty(ids)){
            String[] tenantIds = Convert.toStrArray(ids);
            for(String billId:tenantIds){
                BillInfo thisBillInfo = billInfoService.selectBillInfoById(billId);
                list.add(thisBillInfo);
            }
        }else {
            list = billInfoService.selectBillInfoList(billInfo);
        }
        ExcelUtil<BillInfo> util = new ExcelUtil<BillInfo>(BillInfo.class);
        return util.exportExcel(list, "tenant");
    }





}
